package com.zs.grain.ui

import android.app.Application
import android.content.ComponentName
import android.content.Intent
import android.os.Bundle
import android.os.Handler
import android.widget.Toast
import androidx.lifecycle.MutableLiveData
import com.example.clockkotlin.databaseClockAlarm.AlarmSignal
import com.jeremyliao.liveeventbus.LiveEventBus
import com.zs.grain.*
import javax.inject.Inject
import com.zs.grain.service.DataService
import com.zs.grain.service.DeviceService
import com.xnkj1688.semplibrary.mvvm.BaseViewModel
import com.zs.grain.device.EnumProducingStatus
import com.zs.grain.service.SignalRService
import com.zs.grain.service.SoundService
import com.zs.grain.service.MediaPlayerService
import com.zs.grain.tool.clockManage.LocalDataBase
import com.zslibrary.BusMaster.BusMaster
import java.util.*
import kotlin.collections.ArrayList
import kotlin.concurrent.schedule


class FinishViewModel @Inject constructor(
    val mApp: Application,
    val mDataService: DataService,
    val mDeviceService: DeviceService,
    val mSignalRService: SignalRService,
    val mSoundService: SoundService
) : BaseViewModel(mApp) {
    private var mpService: MediaPlayerService = MediaPlayerService()
    val process = MutableLiveData<String>()
    val downCount = MutableLiveData<Int>()
    var canReturnSell = false
    init {
        process.value = "res://${mApp.packageName}/${R.mipmap.finish}"
        this.downCount.value = 10
        startProgressTimer()
    }

    private var mProgressTimer: Timer? = null
    fun startProgressTimer() {
        this.downCount.postValue(10)
        this.mProgressTimer?.cancel()
        this.mProgressTimer = Timer()
        //mDataService.insertLog("取杯倒计时")
        this.mProgressTimer?.schedule(1000, 1000) {

            var status = mDeviceService.producingStatus
            if(status == EnumProducingStatus.TAKE_AWAY){
                canReturnSell = true
            }
            if(canReturnSell){
                var count = Math.max(0, (downCount.value ?: 1) - 1)
                downCount.postValue(count)
                //if (count <= 0) stopProgressTimer()
            }
        }
    }

    fun init(orderId: Long) {
        //mSoundService.playSound(SoundService.EnumSound.TAKE)
        //mpService?.playMusic2(5)
        mSoundService.playSound2(5)
    }

    fun onFinish(){
        //mDataService.insertLog("State:产品已完成")
    }

    fun stopProgressTimer() {
        this.mProgressTimer?.cancel()
        this.mProgressTimer = null
        //onComplete()
    }

    fun onComplete(){
        mDeviceService.isMakingProduct = false
        //mDataService.insertLog("FVM")
        stopProgressTimer()
        //LiveEventBus.get().with("ProductFinsh.checkTask").post(true)
//        val clocksArray:ArrayList<AlarmSignal> = LocalDataBase.getClocksArray()
//        for(ca in clocksArray){
//            if(ca.setting.contains("update")){
//                Toast.makeText(mApp, "机器即将进入升级状态,请稍等片刻...", Toast.LENGTH_LONG).show()
////                Handler().postDelayed({
////                    var bundle = Bundle()
////                    bundle.putString("UI", "Menus")
////                    bundle.putBoolean("finishProduct",true)
////                    LiveEventBus.get().with("SwitchUI").post(bundle)
////                    canReturnSell = false
////
////                }, 10000)
//                if(ca.setting.contains("updateSetting")){
//                    LiveEventBus.get().with("RemotePush.updateSettings").post(true)
//                }
//                if(ca.setting.contains("updateScene")){
//                    LiveEventBus.get().with("RemotePush.updateScene").post(true)
//                }
////                if(ca.setting.contains("updateAdvs")){
////                    LiveEventBus.get().with("RemotePush.updateAdvs").post(true)
////                }
//            }else if(ca.setting.contains("waitingReboot")){
//                if(ca.enable) {
//                    //uiNotify.postValue(getNotify("RebootTask"))
//                    //if(!mDeviceService.isMachineRemotePusing) {
//                        Toast.makeText(mApp, "机器即将进入维护状态,10秒后自动重启...", Toast.LENGTH_LONG).show()
//                        LocalDataBase.deleteAlarm("waitingReboot")
//                        Handler().postDelayed({
//                            BusMaster.getInstance(mApp).resetBoard()
//                            val componetName = ComponentName(
//                                "com.example.reboottest",
//                                "com.example.reboottest.MainActivity"
//                            );
//                            val intent = Intent();
//                            intent.setComponent(componetName);
//                            mApp.startActivity(intent);
//                        }, 10000)
////                    }else{
////                        Toast.makeText(mApp, "机器仍在升级状态,30秒后自动重启...", Toast.LENGTH_LONG).show()
////                        LocalDataBase.deleteAlarm("waitingReboot")
////                        Handler().postDelayed({
////                            BusMaster.getInstance(mApp).resetBoard()
////                            val componetName = ComponentName(
////                                "com.example.reboottest",
////                                "com.example.reboottest.MainActivity"
////                            );
////                            val intent = Intent();
////                            intent.setComponent(componetName);
////                            mApp.startActivity(intent);
////                        }, 20000)
////                    }
//                }
////                else{
////                    var bundle = Bundle()
////                    bundle.putString("UI", "Menus")
////                    bundle.putBoolean("finishProduct",true)
////                    LiveEventBus.get().with("SwitchUI").post(bundle)
////                    canReturnSell = false
////                }
//            }
//        }

        var bundle = Bundle()
        bundle.putString("UI", "Menus")
        bundle.putBoolean("finishProduct",true)
        LiveEventBus.get("SwitchUI").post(bundle)
        canReturnSell = false
    }

    fun stopMPlayer() {
        if (mpService != null) {
            mpService?.release()
        }
    }

    fun stopPlayer(){
        mpService?.stopPlayer()
    }
}